/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package blancanieves.front;

import blancanieves.back.Blancanieves;
import blancanieves.back.Enanito;
import blancanieves.back.Personaje;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingWorker;

/**
 *
 * @author pabloramix
 */
public class PersonajePanel extends javax.swing.JPanel {

    private Personaje p;    
    private final SwingWorker<Void, Integer> worker;

    public PersonajePanel(Personaje personaje) {
        this.p = personaje;
        initComponents();

        nombreLabel.setText(p.getNombre());
        fotoLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource(p.getFoto())));

        if (p instanceof Enanito) {
            accion1Label.setText("Trabajar:");
            puntosAccion1Label.setText(((Enanito) p).getPuntosTrabajar());
            accion2Label.setText("Comer:");
            puntosAccion2Label.setText(((Enanito) p).getPuntosComer());
            setEstado("PARADO");

        } else if (p instanceof Blancanieves) {
            accion1Label.setText("Cocinar:");
            puntosAccion1Label.setText(((Blancanieves) p).getPuntosCocinar());
            accion2Label.setText("Pasear:");
            puntosAccion2Label.setText(((Blancanieves) p).getPuntosPasear());
            setEstado("PARADA");
        }

        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    doPop(e);
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    doPop(e);
                }
            }

            private void doPop(MouseEvent e) {
                JPopupMenu menu = new JPopupMenu();
                JMenuItem item = new JMenuItem("Iniciar");
                
                item.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseReleased(MouseEvent e) {
                        if (e.getButton() == MouseEvent.BUTTON1) {
                            if (!p.isTerminated()) {
                                p.start();
                            }
                        }
                    }
                });
                
                menu.add(item);
                
                item = new JMenuItem("Parar");
                item.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseReleased(MouseEvent e) {
                        if (e.getButton() == MouseEvent.BUTTON1) {
                            p.terminate();
                        }
                    }
                });
                
                menu.add(item);
                
                if (p instanceof Enanito) {
                
                    final Enanito en = (Enanito) p;
                    
                    item = new JMenuItem("Comer +");
                    item.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseReleased(MouseEvent e) {
                            if (e.getButton() == MouseEvent.BUTTON1) {
                                en.setPrioridadComer(en.getPrioridadComer() + 1);
                                puntosAccion2Label.setText(en.getPuntosComer());
                            }
                        }
                    });
                    
                    menu.add(item);
                    
                    item = new JMenuItem("Comer -");
                    item.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseReleased(MouseEvent e) {
                            if (e.getButton() == MouseEvent.BUTTON1) {
                                en.setPrioridadComer(en.getPrioridadComer() - 1);
                                puntosAccion2Label.setText(en.getPuntosComer());
                            }
                        }
                    });
                    
                    menu.add(item);
                    
                    item = new JMenuItem("Trabajar +");
                    item.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseReleased(MouseEvent e) {
                            if (e.getButton() == MouseEvent.BUTTON1) {
                                en.setPrioridadTrabajar(en.getPrioridadTrabajar() + 1);
                                puntosAccion1Label.setText(en.getPuntosTrabajar());
                            }
                        }
                    });
                    
                    menu.add(item);
                    
                    item = new JMenuItem("Trabajar -");
                    item.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseReleased(MouseEvent e) {
                            if (e.getButton() == MouseEvent.BUTTON1) {
                                en.setPrioridadTrabajar(en.getPrioridadTrabajar() - 1);
                                puntosAccion1Label.setText(en.getPuntosTrabajar());
                            }
                        }
                    });
                    
                    menu.add(item);
                
                } else if (p instanceof Blancanieves) {
                
                    final Blancanieves blan = (Blancanieves) p;
                    
                    item = new JMenuItem("Pasear +");
                    item.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseReleased(MouseEvent e) {
                            if (e.getButton() == MouseEvent.BUTTON1) {
                                blan.setPrioridadPasear(blan.getPrioridadPasear() + 1);
                                puntosAccion2Label.setText(blan.getPuntosPasear());
                            }
                        }
                    });
                    
                    menu.add(item);
                    
                    item = new JMenuItem("Pasear -");
                    item.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseReleased(MouseEvent e) {
                            if (e.getButton() == MouseEvent.BUTTON1) {
                                blan.setPrioridadPasear(blan.getPrioridadPasear() - 1);
                                puntosAccion2Label.setText(blan.getPuntosPasear());
                            }
                        }
                    });
                    
                    menu.add(item);
                    
                    item = new JMenuItem("Cocinar +");
                    item.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseReleased(MouseEvent e) {
                            if (e.getButton() == MouseEvent.BUTTON1) {
                                blan.setPrioridadCocinar(blan.getPrioridadCocinar()+ 1);
                                puntosAccion1Label.setText(blan.getPuntosCocinar());
                            }
                        }
                    });
                    
                    menu.add(item);
                    
                    item = new JMenuItem("Cocinar -");
                    item.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseReleased(MouseEvent e) {
                            if (e.getButton() == MouseEvent.BUTTON1) {
                                blan.setPrioridadCocinar(blan.getPrioridadCocinar() - 1);
                                puntosAccion1Label.setText(blan.getPuntosCocinar());
                            }
                        }
                    });
                    
                    menu.add(item);
                    
                }
                
                menu.show(e.getComponent(), e.getX(), e.getY());
            }
        }
        );
        
        worker = new SwingWorker<Void, Integer>() {
            @Override
            protected Void doInBackground() throws Exception {
                while (true) {
                    setProgress(p.getProgreso());
                    Thread.sleep(10);
                }
            }
        };
        
        worker.addPropertyChangeListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
               barraProgreso.setValue(p.getProgreso());
            }
        });
        
        //worker.execute();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        barraProgreso = new javax.swing.JProgressBar();
        accion1Label = new javax.swing.JLabel();
        accion2Label = new javax.swing.JLabel();
        puntosAccion1Label = new javax.swing.JLabel();
        puntosAccion2Label = new javax.swing.JLabel();
        nombreLabel = new javax.swing.JLabel();
        fotoLabel = new javax.swing.JLabel();
        estadoLabel = new javax.swing.JLabel();

        accion1Label.setText("Trabajar:");
        accion1Label.setName("accion1Label"); // NOI18N

        accion2Label.setText("Comer:");

        puntosAccion1Label.setText("75/100");

        puntosAccion2Label.setText("80/100");

        nombreLabel.setText("Gruñón");

        fotoLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/blancanieves/imagenes/dormilon.png"))); // NOI18N

        estadoLabel.setText("[ESPERANDO]");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(fotoLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(barraProgreso, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(accion1Label)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(puntosAccion1Label)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(accion2Label)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(puntosAccion2Label))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(nombreLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(estadoLabel)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(nombreLabel)
                            .addComponent(estadoLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(barraProgreso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(accion1Label)
                            .addComponent(puntosAccion1Label)
                            .addComponent(accion2Label)
                            .addComponent(puntosAccion2Label)))
                    .addComponent(fotoLabel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel accion1Label;
    private javax.swing.JLabel accion2Label;
    private javax.swing.JProgressBar barraProgreso;
    private javax.swing.JLabel estadoLabel;
    private javax.swing.JLabel fotoLabel;
    private javax.swing.JLabel nombreLabel;
    private javax.swing.JLabel puntosAccion1Label;
    private javax.swing.JLabel puntosAccion2Label;
    // End of variables declaration//GEN-END:variables
    
    public void setProgreso(int i) {
        barraProgreso.setValue(i);
    }
    
    public final void setEstado (String estado) {
        estadoLabel.setText("[" + estado + "]");
    }
    
    public void setPuntos1 (String p) {
        puntosAccion1Label.setText(p);
    }
    
    public void setPuntos2 (String p) {
        puntosAccion2Label.setText(p);
    }
}
